<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Testing extends Command
{
    /**
     * @var VoteService
     */
    private $db;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update-resource';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'update upload resource url';

    public function __construct(DB $db)
    {
        $this->db = $db;
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $items = [
            [
                '1彭丽媛 江山',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/c19ec2fc6b304baaaaf27eb7330e416c/c19ec2fc6b304baaaaf27eb7330e416c_mp3_192k_mp3.mp3',
            ],
            [
                '2谢东 清官谣',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/e58c2c6324a84bcfbead305bc4c67bd0/e58c2c6324a84bcfbead305bc4c67bd0_mp3_192k_mp3.mp3',
            ],
            [
                '3 韩磊 等待',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/409bffc3bd154c719e71b3138de282aa/409bffc3bd154c719e71b3138de282aa_mp3_192k_mp3.mp3'
            ],
            [
                '4戴娆铁齿铜牙纪晓岚',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/dbdb89147445412cac8abd5739d9706f/dbdb89147445412cac8abd5739d9706f_mp3_192k_mp3.mp3'
            ],
            [
                '5刘欢少年壮志不言愁',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/18c5f452eb1445179936d981c65febac/18c5f452eb1445179936d981c65febac_mp3_192k_mp3.mp3',
            ],
            [
                '6 韩磊 众人划桨',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/2684b3d3f6da43deb269018e486026b9/2684b3d3f6da43deb269018e486026b9_mp3_192k_mp3.mp3',
            ],
            [
                '7平安 小梦想大梦想',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/ab561f00601b41f6a00bd282ce6ad0ba/ab561f00601b41f6a00bd282ce6ad0ba_mp3_192k_mp3.mp3',
            ],
            [
                '8江映蓉人民的名义',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/e9ad400443f8434da1b507ae70a99d61/e9ad400443f8434da1b507ae70a99d61_mp3_192k_mp3.mp3',
            ],
            [
                '9韩磊向天再借五百年',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/f92bf8bab13b40b491c81fc8330b3099/f92bf8bab13b40b491c81fc8330b3099_mp3_192k_mp3.mp3',
            ],
            [
                '10 祖海 为了谁',
                'http://video.sobeycloud.com/mfhd/vod/2017/09/16/c550820e0a454b02930afe951a43671d/c550820e0a454b02930afe951a43671d_mp3_192k_mp3.mp3',
            ],
        ];

        foreach ($items as $item) {
            $voteCandidate = $this->findResourceId($item[0]);
            if ($voteCandidate->count() > 0) {
                $resourceId = $voteCandidate[0]->resource_id;
                if  (empty($resourceId)) {
                    $resourceId = md5($item[0]);
                }
                $candidateId = $voteCandidate[0]->id;

                $resourceData = [
                    'resource_url' => $item[1],
                    'candidate_id' => $candidateId,
                    'resource_id'  => $resourceId,
                    'type'         => 3,
                ];
                $ret = $this->createResource($resourceData);

                $this->line(sprintf('Name:%s, URL:%s(%s)', $item[0], $item[1], $ret));
            }
        }
    }

    private function findResourceId($title)
    {
        return DB::table('vote_candidates')
            ->where('vote_candidates.title', '=', $title)
            ->select('resource_id', 'id')
            ->get();
    }

    private function findResource($resourceId)
    {
        return DB::table('resources')
            ->where('resources.resource_id', '=', $resourceId)
            ->select('resource_id')
            ->get();
    }

    private function updateResourceUrl($resourceUrl, $resourceId)
    {
        return DB::table('resources')
            ->where('resources.resource_id', '=', $resourceId)
            ->update(['resource_url' => $resourceUrl]);
    }

    private function createResource($resource)
    {
        return DB::table('resources')
            ->insert($resource);
    }
}
